package com.yzq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzq.common.Result;
import com.yzq.model.dto.comment.CommentsQueryRequest;
import com.yzq.model.dto.comment.CommentsRequest;
import com.yzq.model.entity.Comments;
import com.yzq.model.vo.CommentsVO;
import com.yzq.service.CommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "评论管理")
@RequestMapping("/comments")
@RestController
public class CommentsController {

    @Resource
    private CommentsService commentsService;

    /**
     * 添加评论
     * @param commentsRequest 评论请求
     * @param request 请求
     * @return 评论结果
     */
    @ApiOperation("添加评论")
    @PostMapping("/add")
    public Result<Boolean> addComment(@RequestBody CommentsRequest commentsRequest, HttpServletRequest request) {
        return Result.success(commentsService.addComment(commentsRequest,  request));
    }

    /**
     * 分页获取评论列表
     * @param commentsQueryRequest 评论查询请求
     * @return 评论分页列表
     */
    @ApiOperation("分页获取评论列表")
    @GetMapping("/list")
    public Result<Page<Comments>> getCommentList(CommentsQueryRequest commentsQueryRequest) {
        return Result.success(commentsService.getCommentList(commentsQueryRequest));
    }

    /**
     * 根据图片ID获取评论及其子评论列表
     * @param pictureId 图片ID
     * @return 评论及其子评论列表
     */
    @ApiOperation("根据图片ID获取评论及其子评论列表")
    @GetMapping("/list/all")
    public Result<List<CommentsVO>> getCommentsWithChildrenByPictureId(@RequestParam Long pictureId) {
        List<CommentsVO> commentsVOS = commentsService.getCommentsWithChildrenByPictureId(pictureId);
        return Result.success(commentsVOS);
    }

    /**
     * 删除评论及其所有子评论
     * @param commentId 评论ID
     * @param request 删除请求
     * @return 删除结果
     */
    @ApiOperation("删除评论及其所有子评论")
    @DeleteMapping("/delete")
    public Result<Boolean> deleteComment(@RequestParam Long commentId, HttpServletRequest request) {
        boolean result = commentsService.deleteCommentAndChildren(commentId,  request);
        return Result.success(result);
    }
}
